package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Response;
import com.entity.pojo.WrClockRecord;
import com.entity.pojo.WrUser;
import com.mapper.WrUserMapper;
import com.service.ClockService;
import com.utils.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.support.SecurityContextProvider;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("clock")
public class ClockController {

    @Resource
    private ClockService clockService;

    @Resource
    private WrUserMapper wrUserMapper;

    @PostMapping
    @RequiresRoles("user")
    public Response clockIn(@RequestBody WrClockRecord clockRecord) {
        String username = JwtUtil.getUuid((String) SecurityUtils.getSubject().getPrincipal());
       Integer userId = wrUserMapper.selectOne(new QueryWrapper<WrUser>().eq("username", username)).getId();
        int res = clockService.clockIn(userId, clockRecord.getSchedulingId());
        return res > 0 ? new Response().setSuccess().appendMsg("msg", "操作成功") : new Response().setFailed().setErrMsg("操作失败");
    }
}
